package util;

import java.sql.*;
import java.util.Properties;

/**
 * @author oldliu
 * @since 1.0
 */
public class DBUtil {
    public static  String driver;//="com.mysql.jdbc.Driver";
    public static  String url;//="jdbc:mysql://localhost:3306/java76?useSSL=false&characterEncoding=UTF-8";
    public static  String user;//="root";
    public static  String pass;//="root1234";
    static{
        try {
            Properties p=new Properties();
            p.load(DBUtil.class.getClassLoader().getResourceAsStream("jdbc.properties"));
            driver=p.getProperty("driver");
            url=p.getProperty("url");
            user=p.getProperty("user");
            pass=p.getProperty("pass");

            Class.forName(driver);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    private DBUtil(){}

    public static Connection getConnection(){
        try {
            return DriverManager.getConnection(url,user,pass);
        } catch (SQLException e) {
            e.printStackTrace();
            throw  new RuntimeException(e);
        }
    }
    public static void close(ResultSet rs,Statement s,Connection con){
        close(rs);
        close(s);
        close(con);
    }
    public static void close(ResultSet rs){
        try {
            if(rs!=null &&rs.isClosed()==false)
                rs.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    public static void close(Statement statement){
        try {
            if(statement!=null &&statement.isClosed()==false)
                statement.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    public static void close(PreparedStatement ps){
        try {
            if(ps!=null &&ps.isClosed()==false)
                ps.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    public static void close(Connection connection){
        try {
            if(connection!=null &&connection.isClosed()==false)
                connection.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
